/**
 * Copyright 2009-2015 Mohawk College of Applied Arts and Technology
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License. You may 
 * obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under 
 * the License.
 * 
 * User: fyfej
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace MohawkCollege.EHR.Util.SimpleXSD
{
    /// <summary>
    /// The XmlSchemaElement class represents an XS:Element item
    /// </summary>
    [Description("xs:element")]
    public class XmlSchemaElement : XmlSchemaComplexContent
    {
        private XmlSchemaType type = null;

        /// <summary>
        /// Get the type of element this schema object represents
        /// </summary>
        [ReadOnly(true)]
        public XmlSchemaType SchemaType
        {
            get { return type; }
        }

        public override IEnumerator<XmlSchemaObject> GetEnumerator()
        {
            if(SchemaType is XmlSchemaSimpleType)
                return null;
            else
                return (SchemaType as XmlSchemaComplexType).Content.GetEnumerator();
            
        }

        /// <summary>
        /// Create a new instance of the XmlSchemaElement class
        /// </summary>
        public XmlSchemaElement(XmlSchemaSet schema, XmlSchemaObject parent) : base(schema, parent) { }

        /// <summary>
        /// Load this XmlSchema Element from a .net schema element
        /// </summary>
        /// <param name="element">The element to load from</param>
        public void Load(System.Xml.Schema.XmlSchemaElement element)
        {
            
            // Load base
            base.Load(element);

            this.Name = element.QualifiedName != null ? element.QualifiedName.Name : element.Name;
            this.Namespace = element.QualifiedName != null ? element.QualifiedName.Namespace : Schema.TargetNamespace; 
            this.MinOccurs = element.MinOccursString;
            this.MaxOccurs = element.MaxOccursString;

            // If Schema Type name is null, then that means the element defines an inline type
            if (element.SchemaType != null && element.SchemaType.Name != null)
                this.type = Schema.FindType(element.SchemaType.Name);
            else if(element.ElementSchemaType != null && element.ElementSchemaType.Name != null)
                this.type = Schema.FindType(element.ElementSchemaType.Name);
            else if(element.SchemaType != null)
            {
                Schema.CreateType(element.SchemaType, new XmlQualifiedName(this.Name, this.Namespace));
                this.type = Schema.FindType(element.SchemaType.Name);
                // Weird ...
                if (this.type == null)
                    this.type = Schema.FindType(element.SchemaType.Name);
                this.type.Documentation = "Autogenerated for element";
                this.type.Namespace = this.Namespace;
            }
        }

        public override int Length
        {
            get { return 0; }
        }
    }
}
